<!--
 * @Author: wusd
 * @Date: 2022-06-15 09:39:58
 * @LastEditors: wusd
 * @LastEditTime: 2022-06-16 16:12:08
 * @Description: 
 * 
-->
<template>
  <div class="tabs">
    <el-tag
      v-for="tag in tags"
      :key="tag.name"
      size="medium"
      closable
      :disable-transitions="false"
      @close="handleClose(tag, tags, currentMenue)"
      @click="changeMenu(tag)"
      :effect="$route.name === tag.name ? 'dark' : 'plain'"
      style="position: relative"
    >
      <el-tooltip placement="bottom" effect="light">
        <div slot="content">
          <div class="menu" @click="handleFlushPage(tag)">
            <i class="icon el-icon-refresh"></i>
            <span>刷新</span>
          </div>
        </div>
        <span>
          <i class="el-icon-star-on"></i>
          {{ tag.text }}
        </span>
      </el-tooltip>
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
export default {
  computed: {
    ...mapState({
      tags: state => state.tab.tabsList,
      currentMenue: state => state.tab.currentMenue
    })
  },
  data() {
    return {
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    ...mapMutations({
      close: 'closeTab'
    }),
    handleClose(tag, tags, currentMenue) {
      if (tags.length <= 1) return
      let result = tags.findIndex(item => item.name === tag.name)
      this.close(tag)
      let val
      if (result < tags.length) {
        val = tags[result]
      } else {
        val = tags[tags.length - 1]
      }
      if (currentMenue.name === tag.name) {
        this.$router.push({ name: val.name })
        this.$store.commit('selectMenu', val)
      }
    },
    changeMenu(item) {
      this.$router.push({ name: item.name })
      this.$store.commit('selectMenu', item)
    },
    // 刷新菜单
    handleFlushPage(tag) {
      let query = JSON.parse(JSON.stringify(this.$route.query))
      query.key = Math.random()
      this.$router.replace({ name: tag.name, query })
    }
  }
}
</script>

<style lang="scss" scoped>
.tabs {
  background: #eef2fb80;
  padding: 5px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
.menu {
  padding: 4px 6px;
  cursor: pointer;
  &:hover {
    background-color: #eee;
  }
}
</style>
